package dao.impl;

import com.mysql.cj.util.StringUtils;
import dao.BaseDao;
import dao.EvaluationDao;
import entity.Evaluation;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EvaluationDaoImpl extends BaseDao implements EvaluationDao {

    @Override
    public List<Evaluation> chaxun(int rating, String description, String time)
            throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Evaluation> list = new ArrayList<>();

        try {

            StringBuilder sql = new StringBuilder(
                    "SELECT id, rating, description, evaluation_time, " +
                            "user_nickname, content, evaluation_tags " +
                            "FROM repair_evaluation WHERE 1=1");

            if (rating != 0) {
                sql.append(" AND rating = ?");
            }
            if (!StringUtils.isNullOrEmpty(description)) {
                sql.append(" AND description LIKE ?");
            }
            if (!StringUtils.isNullOrEmpty(time)) {
                switch(time) {
                    case "7":
                        sql.append(" AND evaluation_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)");
                        break;
                    case "30":
                        sql.append(" AND evaluation_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)");
                        break;
                    case "90":
                        sql.append(" AND evaluation_time >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)");
                        break;
                    default:
                        break;
                }
            }

            ps = conn.prepareStatement(sql.toString());

            int paramIndex = 1;
            if (rating != 0) {
                ps.setInt(paramIndex++, rating);
            }
            if (!StringUtils.isNullOrEmpty(description)) {
                ps.setString(paramIndex++, "%" + description + "%");
            }

            rs = ps.executeQuery();
            while (rs.next()) {
                Evaluation evaluation = new Evaluation();
                evaluation.setId(rs.getInt("id"));
                evaluation.setRating(rs.getInt("rating"));
                evaluation.setDescription(rs.getString("description"));
                evaluation.setEvaluationTime(rs.getString("evaluation_time"));
                evaluation.setNickname(rs.getString("user_nickname"));
                evaluation.setContent(rs.getString("content"));
                evaluation.setEvaluationTags(rs.getString("evaluation_tags"));
                list.add(evaluation);
            }
            return list;
        } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (conn != null) conn.close();
        }
    }

    @Override
    public int getTotalCount(int rating, String description, String time)
            throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            StringBuilder sql = new StringBuilder(
                    "SELECT COUNT(*) FROM repair_evaluation WHERE 1=1");

            if (rating != 0) {
                sql.append(" AND rating = ?");
            }
            if (!StringUtils.isNullOrEmpty(description)) {
                sql.append(" AND description LIKE ?");
            }
            if (!StringUtils.isNullOrEmpty(time)) {
                switch(time) {
                    case "7":
                        sql.append(" AND evaluation_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)");
                        break;
                    case "30":
                        sql.append(" AND evaluation_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)");
                        break;
                    case "90":
                        sql.append(" AND evaluation_time >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)");
                        break;
                    default:
                        break;
                }
            }

            ps = conn.prepareStatement(sql.toString());
            int paramIndex = 1;
            if (rating != 0) {
                ps.setInt(paramIndex++, rating);
            }
            if (!StringUtils.isNullOrEmpty(description)) {
                ps.setString(paramIndex++, "%" + description + "%");
            }

            rs = ps.executeQuery();
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (conn != null) conn.close();
        }
    }

    @Override
    public boolean addEvaluation(String orderId, String description, int rating,
                                 String content, String tags, String name)
            throws SQLException, ClassNotFoundException {
      getConnection();
        PreparedStatement ps = null;

        try {

            String sql = "INSERT INTO repair_evaluation(order_id, rating, description, " +
                    "content, evaluation_tags, user_nickname, evaluation_time) " +
                    "VALUES(?, ?, ?, ?, ?, ?, NOW())";
            ps = conn.prepareStatement(sql);
            ps.setString(1, orderId);
            ps.setInt(2, rating);
            ps.setString(3, description);
            ps.setString(4, content);
            ps.setString(5, tags);
            ps.setString(6, name != null ? name : "匿名用户");

            return ps.executeUpdate() > 0;
        } finally {
            if (ps != null) ps.close();
            if (conn != null) conn.close();
        }
    }
}